package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.ContactsheetRj;
import com.ruoyi.huanghefd.domain.LimitWorkApproveSheet;
import com.ruoyi.huanghefd.service.LimitWorkApproveSheetService;
import com.ruoyi.huanghefd.mapper.LimitWorkApproveSheetMapper;
import com.ruoyi.huanghefd.utils.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author gnplum
 * @description 针对表【limit_work_approve_sheet(有限空间作业审批单)】的数据库操作Service实现
 * @createDate 2024-12-03 17:42:16
 */
@Service
public class LimitWorkApproveSheetServiceImpl extends ServiceImpl<LimitWorkApproveSheetMapper, LimitWorkApproveSheet>
        implements LimitWorkApproveSheetService {

    private static final Logger log = LoggerFactory.getLogger(LimitWorkApproveSheetServiceImpl.class);
    @Resource
    private LimitWorkApproveSheetMapper limitWorkApproveSheetMapper;

    @Override
    public AjaxResult addLimitWorkApproveSheet(LimitWorkApproveSheet limitWorkApproveSheet) {
        int result = 0;
        try {
            // 有限空间作业审批单 主键
            String strLsId = UUID.randomUUID().toString().replace("-", "");

            String strLsCode =this.getGenerateOrder("YXZY");
            log.info("生成有限空间作业审批单票号：" + strLsCode);
            limitWorkApproveSheet.setLsCode(strLsCode);
            limitWorkApproveSheet.setLsId(strLsId);

            //插入 有限空间作业审批单 主表
            log.info("新增有限空间作业审批单-开始>>" + limitWorkApproveSheet);
            result = limitWorkApproveSheetMapper.insertAll(limitWorkApproveSheet);
            log.info("新增有限空间作业审批单-结束<<" + result);

            return AjaxResult.success(strLsCode);
        } catch (Exception e) {
            return AjaxResult.error("有限空间作业审批单新增失败", e);
        }
    }

    @Override
    public AjaxResult delLimitWorkApproveSheet(String strLsCode) {
        int result = 0;
        try {
            log.info("删除有限空间作业审批单-开始>>");
            result = limitWorkApproveSheetMapper.delByLsCode(strLsCode);
            log.info("删除有限空间作业审批单-结束<<"+result);

            return AjaxResult.success(strLsCode);
        } catch (Exception e) {
            return AjaxResult.error("有限空间作业审批单删除失败", e);
        }
    }

    @Override
    public AjaxResult uptLimitWorkApproveSheet(LimitWorkApproveSheet limitWorkApproveSheet) {
        int result = 0;
        try {
            log.info("修改有限空间作业审批单-开始>>");
            result = limitWorkApproveSheetMapper.updateByLsId(limitWorkApproveSheet);
            log.info("修改有限空间作业审批单-结束<<"+result);

            return AjaxResult.success(limitWorkApproveSheet.getLsCode());
        } catch (Exception e) {
            return AjaxResult.error("修改有限空间作业审批单失败", e);
        }
    }

    @Override
    public PageBean queryLimitWorkApproveSheetByPage(String strLsCode, String strLsDept, String strLsState, String strUserId, String strMailType, int pageNum, int pageSize) {
        PageBean pageBean = new PageBean();
        if (pageNum > 0 && pageSize > 0) {
            int totalRecord = limitWorkApproveSheetMapper.getLimitWorkApproveSheetNum(strLsCode, strLsDept, strLsState, strUserId, strMailType);
            pageBean = new PageBean(pageNum, pageSize, totalRecord);

            //从该行开始拿数据
            int startIndex = (pageNum - 1) * pageSize;
            pageBean.setStartIndex(startIndex);
            pageBean.setCode(200);

            //数据集合
            List<LimitWorkApproveSheet> limitWorkApproveSheetList = limitWorkApproveSheetMapper.queryLimitWorkApproveSheetByPage(strLsCode, strLsDept, strLsState, strUserId, strMailType,startIndex, pageSize);
            pageBean.setList(limitWorkApproveSheetList);
            pageBean.setSuccessMsg("有限空间作业审批单查询成功！");
        } else {
            pageBean.setCode(500);
            pageBean.setFailMsg("有限空间作业审批单查询失败！");
        }
        return pageBean;
    }

    private String getGenerateOrder(String Type) {

        log.info("获取票编号=" + Type);
        String strWsCode = "";
        int order = 0;
        try {

            order = limitWorkApproveSheetMapper.getLsCodeNum(Type);
            log.info("order=" + order);
            //DQ1202311110001
            strWsCode = Type + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%03d", order);

            log.info("strWsCode=" + strWsCode);

        } catch (Exception e) {
            strWsCode = "";
            //e.printStackTrace();
            throw  e;
            //log.error(e.getCause()+e.getMessage());
        }
        return strWsCode;
    }
}




